﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.DevHelper.FeedStudio.Model;
using EzDesk.DevHelper.FeedStudio.Views;
using System.Threading;
using EzDesk.Utility.Log;
using System.Xml.Linq;
using System.Diagnostics;
using System.Windows.Forms;

namespace EzDesk.DevHelper.FeedStudio.Presenter
{
    public class FeedMainPresenter
    {
        private IFeedMainView _view;

        public FeedMainPresenter(IFeedMainView view)
        {
            _view = view;
        }

        public void RefreshFeedList()
        {
            LogEx.TRACE("MainPresenter", "获取FEED列表");
            //获取Feed列表);
            using (var db = new EzDeskDatabaseDataContext())
            {
                var feedList = db.ez_FeedTemplates
                    .Select(p => new
                             {
                                 TemplateName = p.TemplateName,
                             })
                             .ToList();

                LogEx.TRACE("MainPresenter", "获取FEED列表成功");
                //显示Feed列表
                _view.DisplayFeedTemplatesList(feedList);
            }

        }

        public string AddNewFeed()
        {
            LogEx.TRACE("MainPresenter", "增添FEED");

            var templateName = getNewTemplateName();

            using (var db = new EzDeskDatabaseDataContext())
            {
                db.ez_FeedTemplates.InsertOnSubmit(
                    new ez_FeedTemplate()
                    {
                        //todo..
                        TemplateName = templateName,
                        TempleId = Guid.NewGuid(),
                        DefaultType = 0

                    });

                db.SubmitChanges();
                LogEx.TRACE("MainPresenter", "增添FEED成功");
            }


            RefreshFeedList();

            return templateName;

        }




        public void DeleteFeed(string selectFeedName)
        {
            using (var db = new EzDeskDatabaseDataContext())
            {
                LogEx.TRACE("MainPresenter", "删除FEED");
                var feed = db.ez_FeedTemplates
                    .Where(p => p.TemplateName == selectFeedName);

                db.ez_FeedTemplates.DeleteAllOnSubmit(feed);
                db.SubmitChanges();

                LogEx.TRACE("MainPresenter", "删除FEED成功");

                RefreshFeedList();
            }

        }




        private string getNewTemplateName()
        {
            return "新Feed" + " " + DateTime.Now.Ticks.ToString();
        }



        private string exportXmlFileName = "EzDeskFeedTemplates.xml";

        public void ExportFeedTemplateXml()
        {
            LogEx.TRACE("MainForm - Presenter", "开始将当前数据库内容导出到XML");

            XElement saveXml = new XElement("FeedTemplates");

            using (var db = new EzDeskDatabaseDataContext())
            {
                foreach (var entity in db.ez_FeedTemplates)
                {
                    XElement feedTempalteEle = new XElement("feedTemplate");
                    feedTempalteEle.Add(new XElement("id", entity.TempleId.ToString()));
                    feedTempalteEle.Add(new XElement("name", entity.TemplateName));
                    feedTempalteEle.Add(new XElement("body", entity.Body));
                    feedTempalteEle.Add(new XElement("title", entity.Title));
                    feedTempalteEle.Add(new XElement("type", entity.DefaultType.ToString()));
                    feedTempalteEle.Add(new XElement("templateIcon", entity.TemplateIcon));

                    saveXml.Add(feedTempalteEle);
                }
            }

            saveXml.Save(exportXmlFileName);
            Process.Start("explorer.exe", Application.StartupPath); 
        }
    }
}
